/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.hanserver.action.hancadpo.getdatagrid;

import br.com.sales.hanserver.action.hancadpe.GetDataPersonGridAction;
import static br.com.sales.hanserver.action.hancadpo.consts.HanCadPOServerConsts.GET_PRODUCTS;
import static br.com.sales.hanserver.action.hancadpo.consts.HanCadPOServerConsts.GET_LAST_PRODUCTS;
import static br.com.sales.hanserver.action.hancadpo.consts.HanCadPOServerConsts.GET_PRODUCTS_UPDATE;
import br.com.sales.hanserver.dao.Dao;
import static br.com.sales.hanserver.dao.Dao.closeConn;
import static br.com.sales.hanserver.dao.Dao.getConn;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author v
 */
public class GetDataProductGridAction extends Dao {

    public GetDataProductGridAction() {
    }

    public List<Map> getDataGrid(int page) {
        List<Map> resultData = new LinkedList<>();
        try {
            PreparedStatement st = getConn().prepareStatement(GET_PRODUCTS);
            st.setInt(1, page);

            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Map map = new LinkedHashMap(3);
                map.put("SEQ", rs.getInt("SEQ"));
                map.put("DT_REGISTRO", rs.getString("DT_REGISTRO"));
                map.put("DS_PRODUTO", rs.getString("DS_PRODUTO"));
                map.put("VL_VENDA", rs.getString("VL_VENDA"));
                map.put("QT_ESTOQUE", rs.getInt("QT_ESTOQUE"));
                map.put("FORNEC", rs.getString("FORNEC"));
                map.put("VL_COMPRA", rs.getString("VL_COMPRA"));
                map.put("VL_PROMOCIONAL", rs.getString("VL_PROMOCIONAL"));
                map.put("PR_MARGEM_LUCRO", rs.getString("PR_MARGEM_LUCRO"));
                map.put("QT_MAX_ESTOQUE", rs.getString("QT_MAX_ESTOQUE"));
                map.put("QT_MIN_ESTOQUE", rs.getString("QT_MIN_ESTOQUE"));
                map.put("DS_BREVE", rs.getString("DS_BREVE"));
                map.put("IE_ESTOQUE", rs.getString("IE_ESTOQUE"));
                map.put("NR_SEQ_PF", rs.getString("NR_SEQ_PF"));
                resultData.add(map);
            }

            return resultData;

        } catch (Exception ex) {
            Logger.getLogger(GetDataProductGridAction.class.getName()).log(Level.SEVERE, "Error getDataGrid", ex);
        } finally {
            closeConn();
        }
        return resultData;
    }

    public Map getLastProduct(Long sequence) {
        Map map = new LinkedHashMap(3);
        try {
            Statement st;
            ResultSet rs;

            if (sequence > 0) {
                st = getConn().prepareCall(GET_PRODUCTS_UPDATE);
                ((PreparedStatement) st).setLong(1, sequence);
                rs = ((PreparedStatement) st).executeQuery();
            } else {
                st = getConn().createStatement();
                rs = st.executeQuery(GET_LAST_PRODUCTS);
            }

            while (rs.next()) {
                map.put("SEQ", rs.getInt("SEQ"));
                map.put("DT_REGISTRO", rs.getString("DT_REGISTRO"));
                map.put("DS_PRODUTO", rs.getString("DS_PRODUTO"));
                map.put("VL_VENDA", rs.getString("VL_VENDA"));
                map.put("QT_ESTOQUE", rs.getInt("QT_ESTOQUE"));
                map.put("FORNEC", rs.getString("FORNEC"));
                map.put("VL_COMPRA", rs.getString("VL_COMPRA"));
                map.put("VL_PROMOCIONAL", rs.getString("VL_PROMOCIONAL"));
                map.put("PR_MARGEM_LUCRO", rs.getString("PR_MARGEM_LUCRO"));
                map.put("QT_MAX_ESTOQUE", rs.getString("QT_MAX_ESTOQUE"));
                map.put("QT_MIN_ESTOQUE", rs.getString("QT_MIN_ESTOQUE"));
                map.put("DS_BREVE", rs.getString("DS_BREVE"));
                map.put("IE_ESTOQUE", rs.getString("IE_ESTOQUE"));
                map.put("NR_SEQ_PF", rs.getString("NR_SEQ_PF"));
            }

            return map;

        } catch (Exception ex) {
            Logger.getLogger(GetDataPersonGridAction.class.getName()).log(Level.SEVERE, "Error getLastPerson", ex);
        } finally {
            closeConn();
        }
        return map;
    }
}
